### THIS CODE PRODUCES THE DELEGATION AND CONSTRAINT RATIOS BY CELEX #
# Let's create delegation and constraint ratios by month and then by year

# Delegation by month

library(foreign,ggplot2,dplyr)

# These are the coded provisions produced by he GBT classifiers.
classdata = read.csv(
  "~/Output Data/coded_provisions_ms.csv"
)

monthlydata = paste(classdata$newprovision.year,classdata$newprovisiondata.month,sep=".")

se<-function(x){
  sd(x)/sqrt(length(x))
}

# First by celex and month, then by month
delegationbymonth.mean.celex = aggregate(data.frame(classdata$delegation.predictions,classdata$delegation.probs),
                              by = list(classdata$newprovision.celex, classdata$newprovision.year,classdata$newprovisiondata.month),
                              FUN = mean)


#### Delegation ratio by CELEX
delegation.ratio.celex = delegationbymonth.mean.celex$classdata.delegation.predictions
#### Delegation ratio by CELEX


#### Let's just create a new dataset with the celex level data, year and month identifiers
celex.out = delegationbymonth.mean.celex$Group.1
month.out = delegationbymonth.mean.celex$Group.3
year.out = delegationbymonth.mean.celex$Group.2



## Now let's produce constraint ratios
## There are 8 total constraints here so the first step is to count the total constraints by celex

odds = seq(from = 6, to = 20, by = 2)
constraintpreds = classdata[,odds]

constraintssums = aggregate(
  constraintpreds, by = list(classdata$newprovision.celex, monthlydata), FUN=sum
)

# For each column, do an ifelse >=1 

for(i in 3:10){
  constraintssums[,i]<-ifelse(constraintssums[,i] >= 1, 1,0)
}

# Now the sum of the columns are the number of constraints
nconstraints = rowSums(constraintssums[,3:10])

# Constraint ratio is now nconstraints/12

constraint.ratio = nconstraints/12

# Fold this back into the contraintssums data
constraintssums = data.frame(constraintssums,constraint.ratio)

###### Constraint ratio by celex
constraint.ratio.celex = constraintssums$constraint.ratio
###### Constraint ratio by celex

# Let's create the discretion index
discretion = delegation.ratio.celex - (constraint.ratio.celex*delegation.ratio.celex)

#### Now write out a new data frame with the celex and the ratios

final.ratio.data = data.frame(celex.out,
                              year.out,
                              month.out,
                              delegation.ratio.celex,
                              constraint.ratio.celex,
                              discretion)

names(final.ratio.data)<-c("celex","year","month","delegation.ratio","constraint.ratio", "discretion.index")


# These are the final delegation/constraint/discretion ratios produced from the classified provisions
write.csv(final.ratio.data,
  "~/Output Data/discretion-delegation-constraint_MS.csv")





plot(final.ratio.data$delegation.ratio,final.ratio.data$discretion.index)






